package web

import (
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
	"net/http"
	"testPaper/common"
	"testPaper/model"
	"testPaper/response"
)

// CreatNewTestPaper 历年真题
func CreatNewTestPaper(c *gin.Context) {
	DB := common.GetDB()
	name := c.PostForm("name")
	content := c.PostForm("content")
	money := c.PostForm("money")
	UseTime := c.PostForm("useTime")
	img := c.PostForm("img")

	if len(name) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "试卷名称不能为空")
		return
	} else if len(content) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "请输入试卷简介")
		return
	} else if len(money) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "请输入试卷价格")
		return
	} else if len(UseTime) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "请输入试卷会员使用日期")
		return
	} else if len(img) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "请上传试卷封面")
		return
	}
	//生成随机id
	guidStr := uuid.NewV1().String()

	newTestPaper := model.NewTestPaper{
		Name:     name,
		PaperId:  guidStr,
		Content:  content,
		Money:    money,
		UserTime: UseTime,
		Img:      img,
	}
	DB.Create(&newTestPaper)
	response.Reponse(c, http.StatusOK, 200, nil, "创建试卷成功")
	return
}
